<?php defined('SYSPATH') OR die('No direct access allowed.');

class Account_Controller extends Frontend_Controller {

	public $history = TRUE;
	protected static $actions = array();
	//public $template = 'account/main';

	public function __construct() {
		parent::__construct();
		if ($this->user) {
			self::$actions = array
			(
				'info'			=> 'account.actions.info',
				'password'	=> 'account.actions.change_password',
				'settings'	=> 'account.actions.settings',
/*				'index'			=> 'account.actions.all_users',*/
			);
		}
/*		else
			self::$actions = array
			(
				'index'			=> 'account.actions.all_users',
			);*/
		$this->template->head['css']['media/css/auth.css'] = 'all';
		$this->template->page = View::factory('account/main')->bind('actions', self::$actions);
		$this->template->header = View::factory('layout/header');
		$this->template->footer = new View('layout/footer');
	}

	public function info() {
		if (!isset(Router::$arguments['user'])) {
			if ($this->user) $id = $this->user->id;
			else return $this->index();
		}
		else $id = Router::$arguments['user'];
		$user = ORM::factory('user')->with('user_detail')->find($id);
		if (!$user->loaded) {
			// user not found!
			return $this->_not_found($id);
		}
		$this->template->head['title'] = Kohana::lang('auth.profile_title').$user->username;
		$this->template->page->content = View::factory('account/view')->set('user', $user);
	}

	public function index() {
		// show all users
		$page_num = $this->input->get('pages', 1);
		$per_page = Kohana::config('cms.user_per_page');
		$offset = ($page_num - 1)*$per_page;
		$user = ORM::factory('user');
		$users = $user->limit($per_page, $offset)->find_all();
		$pages = Pagination::factory(array
			(
				'style' => Kohana::config('cms.pagination_style'),
				'items_per_page' => $per_page,
				'query_string' => 'page',
				'total_items' => $user->count_last_query(),
			));
		$this->template->head['title'] = Kohana::lang('auth.account_list');
		$this->template->page->content = View::factory('account/index')->bind('users', $users);
	}

	public function _not_found($id) {
		// user not found
	}

}